package com.black.auth.client.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.black.auth.client.po.OauthClientDetails;
import com.black.auth.client.service.OauthClientDetailsService;
import com.black.common.web.ApiResponse;
import com.black.common.web.ApiResponseBuilder;
import com.black.common.web.PageQuery;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author linwenfeng
 */
@RestController
@RequestMapping("client")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ClientController {

    private final OauthClientDetailsService oauthClientDetailsService;

    @PostMapping("search")
    public ApiResponse<IPage<OauthClientDetails>> search(@RequestBody PageQuery<OauthClientDetails> searchVo) {
        return ApiResponseBuilder.ok(oauthClientDetailsService.search(searchVo));
    }

    @GetMapping("Id/{clientId}")
    public ApiResponse<OauthClientDetails> getById(@PathVariable("clientId") String clientId) {
        return ApiResponseBuilder.ok(oauthClientDetailsService.findById(clientId));
    }


    @PostMapping("save")
    public ApiResponse<String> save(@RequestBody OauthClientDetails clientDetails) throws Exception {
        oauthClientDetailsService.createOauthClientDetails(clientDetails);
        return ApiResponseBuilder.ok("新增成功");
    }

    @PostMapping("update")
    public ApiResponse<String> update(@RequestBody OauthClientDetails clientDetails) {
        oauthClientDetailsService.updateOauthClientDetails(clientDetails);
        return ApiResponseBuilder.ok("更新成功");
    }

    @DeleteMapping
    public ApiResponse<String> delete(@RequestBody List<String> clientIds) {
        oauthClientDetailsService.deleteOauthClientDetails(clientIds);
        return ApiResponseBuilder.ok("删除成功");
    }

}
